//
//  GameSceneObject.m
//  Legions
//
//  Created by Anders Petersen on 12/05/14.
//  Copyright (c) 2014 Anders Petersen. All rights reserved.
//

#import "Army.h"
#import "Soldier.h"
#import "ArmySymbol.h"
#import "MathUtilities.h"

@implementation Army

//formation 1- cluster
int xF1 = ARMY_SIZE/6;
int yF1 = ARMY_SIZE/6;



- (instancetype)init
{
    self = [super init];
//    self = [Army spriteNodeWithImageNamed:@"knight"];
//    self.alpha = .5;
//    self = [Army spriteNodeWithColor:[SKColor colorWithRed:0 green:0 blue:0 alpha:.2] size:CGSizeMake(ARMY_SIZE, ARMY_SIZE)];
    if (self) {
        [self initObjects];
    }
    return self;
}

-(void)initObjects{
    
    self.armySymbol = [[ArmySymbol alloc] init];
    [self addChild:self.armySymbol];

    
    self.soldier1 = [[Soldier alloc] init];
    [self addChild:self.soldier1];

      
    self.soldier2 = [[Soldier alloc] init];
    [self addChild:self.soldier2];

    self.soldier3 = [[Soldier alloc] init];
    [self addChild:self.soldier3];
    
    self.soldier4 = [[Soldier alloc] init];
    [self addChild:self.soldier4];

    self.soldier5 = [[Soldier alloc] init];
    [self addChild:self.soldier5];
    
    [self.soldier1 setPosition:CGPointMake(xF1, yF1)];
    [self.soldier2 setPosition:CGPointMake(-xF1, yF1)];
    [self.soldier3 setPosition:CGPointMake(0, 0)];
    [self.soldier4 setPosition:CGPointMake(xF1, -yF1)];
    [self.soldier5 setPosition:CGPointMake(-xF1, -yF1)];   
    
    [self setSize:CGSizeMake(ARMY_SIZE, ARMY_SIZE)];
    self.zPosition = ARMY_SIZE;
}

-(void) moveTo:(CGPoint)position with: (CGFloat) duration;{
    

    CGFloat rad = [MathUtilities angleBetween:self.position and:position];
    
    float rotate    =   self.angle - rad;
    
    if(rotate > M_PI){
        rotate = rotate - 2*M_PI;
    }
    if(rotate < -M_PI){
        rotate = rotate + 2*M_PI;        
    }
    
    
    duration = [self getDurationPosition1:self.position and2:position];
    
    
    float moveT     =   duration;
    float turnT     =   duration/4;
    
    NSLog(@"%f %f %f", self.zRotation*180/M_PI, rad*180/M_PI , rotate*180/M_PI);
    
    [self removeAllActions];
    [self runAction:[SKAction rotateByAngle:rotate duration:turnT]];
    [self runAction:[SKAction moveTo:position duration:moveT]];
    
    [self.soldier1 turnRad:-rotate with:turnT];
    [self.soldier2 turnRad:-rotate with:turnT];
    [self.soldier3 turnRad:-rotate with:turnT];
    [self.soldier4 turnRad:-rotate with:turnT];
    [self.soldier5 turnRad:-rotate with:turnT];
    
    [self.soldier1 runAction:[SKAction moveTo:CGPointMake(xF1, yF1) duration:moveT]];
    [self.soldier2 runAction:[SKAction moveTo:CGPointMake(-xF1, yF1) duration:moveT]];
    [self.soldier3 runAction:[SKAction moveTo:CGPointMake(0, 0) duration:moveT]];
    [self.soldier4 runAction:[SKAction moveTo:CGPointMake(xF1, -yF1) duration:moveT]];
    [self.soldier5 runAction:[SKAction moveTo:CGPointMake(-xF1, -yF1) duration:moveT]];

    [self.armySymbol runAction:[SKAction rotateByAngle:-rotate duration:turnT]];


    
    self.angle = rad;
    
}


-(float) getDurationPosition1:(CGPoint) pos1 and2:(CGPoint) pos2{
    
    float length = sqrtf((pos1.x - pos2.x)*(pos1.x - pos2.x) + (pos1.y - pos2.y)*(pos1.y - pos2.y));
    
    float duration = length/80.0f;
    
    return duration;
}
 


-(GameObject *)getGameObjectParent{
    return ((GameObject *)self);
}


@end
